import {Injectable} from '@angular/core';
import {Api} from './api.service';
import {Router} from '@angular/router';
import {SnackService} from './snack.service';
import {MatSnackBar} from '@angular/material';
import {SnackSuccessComponent} from '../page/snack-success/snack-success.component';
import {SnackErrorComponent} from '../page/snack-error/snack-error.component';
import {Position} from '../entity/position';
import {AuthService} from './auth.service';

@Injectable()
export class CompanyService {
  constructor(
    public api: Api,
    public snackBar: MatSnackBar,
    public snackService: SnackService,
    public router: Router,
    public authService: AuthService
  ) {}
  //  增加职位
  addPosition(position: Position) {
    position.companyId = this.authService.company.id;
    const seq = this.api.post2('company/addPosition', position);
    // debugger
    seq.subscribe((res: any) => {
      if (res == 'OK') {
        this.snackService.message = '添加成功';
        this.snackBar.openFromComponent(SnackSuccessComponent, {
          duration: 3000,
        });
        // this.returnLogin();
      } else {
      }
    }, err => {
      console.error('ERROR', err);
      this.snackService.message = err.error.message;
      this.snackBar.openFromComponent(SnackErrorComponent, {
        duration: 3000,
      });
    });

    return seq;
  }
  //  修改职位
  editPosition(position: Position) {
    let data = {data: JSON.stringify(position), companyId: this.authService.company.id};
    const seq = this.api.post('company/editPosition', data);
    // debugger
    seq.subscribe((res: any) => {
      if (res == 'OK') {
        this.snackService.message = '修改成功';
        this.snackBar.openFromComponent(SnackSuccessComponent, {
          duration: 3000,
        });
        // this.returnLogin();
      } else {
      }
    }, err => {
      console.error('ERROR', err);
      this.snackService.message = err.error.message;
      this.snackBar.openFromComponent(SnackErrorComponent, {
        duration: 3000,
      });
    });

    return seq;
  }
  // 查询职位列表
  findPositonList() {
    let data = {companyId: this.authService.company.id};
    return this.api.post('company/positionList', data);
  }
  // 查询职位
  findPositonOne(id: number) {
    let data = {id: id};
    return this.api.post('company/findPositionOne', data);
  }
  // 删除职位
  delPosition(id: number) {
    let data = {id: id, companyId: this.authService.company.id};
    return this.api.post('company/delPosition', data);
  }
  // 分页查询
  findAll(pageable: any) {
    return this.api.post('company/findAll', pageable);
  }
  // 分页查询
  findAllLike(pageable: any) {
    return this.api.post('company/findAllLike', pageable);
  }
  // 申请列表
  appList() {
    let data = {companyId: this.authService.company.id};
    return this.api.post('company/appList', data);
  }
  // 申请处理
  applicationProcessing(process: string, appId: number) {
    let data = {process: process, appId: appId};
    const seq = this.api.post('company/appProcess', data);

    return seq;
  }
  // 查询公司资料
  findCompanyInfo(companyId: number) {
    let data = {companyId: companyId};
    const seq = this.api.post('company/findCompanyInfo', data);

    return seq;
  }
}
